package com.example.oj_springboot.controller;

import com.example.oj_springboot.api.ProblemRest;
import com.example.oj_springboot.api.config.exception.CodeNullException;
import com.example.oj_springboot.api.config.exception.IdNullException;
import com.example.oj_springboot.model.Problem;
import com.example.oj_springboot.service.ProblemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class ProblemController implements ProblemRest {

    @Autowired
    private ProblemService problemService;

    @Override
    public Problem getProblem(String id) throws IdNullException {
        if (StringUtils.isBlank(id)){
            throw new IdNullException();
        }
        return problemService.getOne(id);
    }

    @Override
    public List<Problem> getProblems(){
        return problemService.getAll();
    }

    @Override
    public void addProblem(Problem problem) throws CodeNullException {
        if (StringUtils.isBlank(problem.getTitle())|| StringUtils.isBlank(problem.getDescription()) || StringUtils.isBlank(problem.getTestCode())) {
            throw new CodeNullException();
        }
        problemService.add(problem);
    }

    @Override
    public void delete(String id) throws IdNullException {
        if (StringUtils.isBlank(id)){
            throw new IdNullException();
        }
        problemService.delete(id);
    }
}